package com.xiaochong.bitong.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.api.config.hbase.HbaseBaseQueryComponent;
import com.xiaochong.bitong.api.constant.ReturnCode;
import com.xiaochong.bitong.api.utils.ListSortPageUtil;
import com.xiaochong.bitong.api.utils.ListSortUtil;
import com.xiaochong.bitong.api.utils.StringUtil;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.hbasepo.*;
import com.xiaochong.tir.common.data.mapper.*;
import com.xiaochong.tir.common.data.po.*;
import com.xiaochong.tir.common.data.redisPo.KActionTotalPo;
import com.xiaochong.tir.common.data.vo.*;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Semaphore;

/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @author mengxiangdong
 * @time 2018-05-23 14:07
 *
 */
@Service
public class QuotationService {

    @Autowired
    private RedisServer redisServer;

    @Autowired
    private CurrencyInfoMapper currencyInfoMapper;

    @Autowired
    private CurrencyIndustryCategoryMapper currencyIndustryCategoryMapper;

    @Autowired
    private HbaseBaseQueryComponent hbaseBaseQueryComponent;

    @Autowired
    private ConceptCategoryMapper conceptCategoryMapper;

    @Autowired
    private ExchangeInfoMapper exchangeInfoMapper;

    @Autowired
    private SelfSelectedMapper selfSelectedMapper;

    @Autowired
    CoinSummarizeAffiliatedMapper coinSummarizeAffiliatedMapper;

    @Autowired
    CurrencyInfoHbaseMapper currencyInfoHbaseMapper;

    private static Semaphore semaphore = new Semaphore(1);

    /**
     * 行情k线
     * @param kActionQuantizationParamsVo
     * @return
     */
    public ApiResultVo<List<ApiKActionQuantizationVo>> kActionQuantization(int pageIndex,int pageSize,String field,String type,int exchangeId) {
        Long st = System.currentTimeMillis();
        ApiResultVo<List<ApiKActionQuantizationVo>> resultVo = new ApiResultVo<>();
        resultVo.setDataStamp(System.currentTimeMillis());
        resultVo.setTradeId(LogTrace.getTrace().getTraceId());

        try {
            String redisKey = RedisKeysUtils.API_SERVICE_TRADES_EXCHANGE_SELECT_ALL_DATA+"@";
            redisKey +="exchange@"+exchangeId;
            String data = redisServer.get(redisKey);
            List<ApiKActionQuantizationVo> dataVo = new ArrayList<>();
            if(StringUtils.isNotBlank(data)){
                dataVo = JSON.parseArray(data,ApiKActionQuantizationVo.class);
            }
            if(dataVo == null || dataVo.size() == 0){
                resultVo.setCode(ReturnCode.DATA_NOT_EXIST);
                return resultVo;
            }
            //排序--根据排序取出对应的条数对应的页数的数据返回前端
            if(StringUtils.isBlank(field)){//设置默认排序字段是当前价格
                field="amount";
            }
            if(StringUtils.isBlank(type)){//设置默认排序为降序
                type="desc";
            }
            ListSortUtil util=new ListSortUtil();
            util.mySort( dataVo, field.trim(), type.trim() );
            //排名
            if (dataVo.size()>0){
                for (int i=0;i<dataVo.size();i++){
                    ApiKActionQuantizationVo apiKActionQuantizationVo = dataVo.get( i );
                    apiKActionQuantizationVo.setRank( i+1 );
                }
            }

            //插入分页默认值分页
            if(pageIndex==0){
                pageIndex=1;
            }
            if(pageSize==0){
                pageSize=15;
            }
            List<ApiKActionQuantizationVo> result= ListSortPageUtil.pageBean(dataVo,pageIndex,pageSize);

            //计算页数
            int pages = dataVo.size()/pageSize;
            int yu = dataVo.size()%pageSize;
            if(yu > 0){
                pages = pages+1;
            }
            resultVo.setData(result);
            resultVo.setCode(ReturnCode.SUCCESS);
            resultVo.setPages(pages);
            resultVo.setTotal(dataVo.size());
        }catch (Exception e){
            LogTrace.error("行情k线",e);
            e.printStackTrace();
            resultVo.setCode(ReturnCode.ERROR);
        }
        LogTrace.info("行情k线api","总耗时",String.valueOf(System.currentTimeMillis()- st));
        return resultVo;
    }

}